<!--
 * @Description: 站点配比维护
 * @Author: wangwangwang
 * @Date: 2020-09-21 14:09:06
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-12-29 14:30:37
-->
<template>
  <div class="pages_content table">
    <el-container>
      <el-header>
        <breaDcrumb />
      </el-header>
      <el-main style="display:flex">
        <div v-loading="treeLoading" class="tree">
          <el-tree ref="tree" :data="treeData" default-expand-all :expand-on-click-node="false">
            <span slot-scope="{ node,data }" class="custom-tree-node tree-line">
              <span v-if="data.children">
                <i :class="data.icon"></i>
                <span>{{ node.label }}</span>
              </span>
              <el-radio-group v-else v-model="activetree" @change="changeStation(node.label)">
                <el-radio :label="data.value">
                  <span>{{ node.label }}</span>
                </el-radio>
              </el-radio-group>
            </span>
          </el-tree>
        </div>
        <div>
          <div class="header_right" @click="showCardNav = !showCardNav">
            <div class="title">搜索</div>
            <div>
              <transition name="from"></transition>
              <span v-show="!showCardNav">
                <i style="margin-right: 5px;" class="el-icon-arrow-down"></i>
              </span>
              <transition name="to"></transition>
              <span v-show="showCardNav">
                <i style="margin-right: 5px;" class="el-icon-arrow-up"></i>
              </span>
            </div>
          </div>
          <div v-show="showCardNav" class="hr"></div>
          <el-collapse-transition>
            <div v-show="showCardNav">
              <div class="search">
                <span style="display: inline-block;margin-bottom: 10px;">
                  <span style="margin-right: 12px;">原物料名称</span>
                  <el-select v-model="tableParam.rawId" placeholder="请选择">
                    <el-option v-for="(item, index) in arr1" :key="index" :label="item.itemName" :value="item.id"></el-option>
                  </el-select>
                </span>
                <el-button icon="el-icon-search" size="medium" class="bottom_all" @click="(tableParam.page = 1), gettableData()">查 询</el-button>
                <el-button size="medium" class="bottom_all_two" style="margin-left:12px;" @click="init()">重 置</el-button>
              </div>
            </div>
          </el-collapse-transition>
          <div class="hr"></div>
          <div class="card">
            <div class="card_nav">
              <div>
                <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='TIANJIA'))!==-1" size="small" class="bottom_all" style="margin-left: 0;" @click="addFromDate('添加')">
                  <i class="icon iconfont iconicon_button_add" style="font-size: 12px;"></i>
                  添加
                </el-button>
              </div>
            </div>
            <div v-loading="tableLoading" class="card_info" :style="style">
              <el-table :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width:100%" :data="tableData" tooltip-effect="dark">
                <template v-for="(item, index2) in table">
                  <el-table-column v-if="item.status" :key="index2" :label="item.label" align="center" :width="item.width">
                    <template slot-scope="scope">
                      <el-button v-if="item.label == '操作'&&ButtonList.findIndex(res=>(res.buttonValue=='XIUGAI'))!==-1" class="bottom_one" size="mini" @click="updateFromDate(scope.row,'修改'),oldData = scope.row">
                        <i class="icon iconfont iconicon_button_modification" style="font-size: 12px;"></i>
                        修改
                      </el-button>
                      <span v-else>{{ scope.row[item.prop] }}</span>
                    </template>
                  </el-table-column>
                </template>
              </el-table>
            </div>
          </div>
        </div>
      </el-main>
      <div class="card_bottom">
        <div class="card_bottom_left">
          <div class="top_page_totle">总共{{ total }}条</div>
          <el-pagination :current-page.sync="tableParam.page" :page-size="tableParam.pageSize" :page-sizes="[10, 20, 30, 40,50,100,200,500,1000]" layout="prev,pager,sizes,next,jumper" :total="total" @size-change="sizeChange" @current-change="gettableData()"></el-pagination>
        </div>
        <div>
          <el-button size="mini" icon="el-icon-refresh-right" class="bottom_all_two" @click="gettableData">刷新</el-button>
        </div>
      </div>
    </el-container>
    <el-dialog v-dialogDrag :visible.sync="addDialog" width="480px" :close-on-click-modal="false" :before-close="resetFromDate">
      <span slot="title" class="dialog-title">{{ addDialogTitle }}</span>
      <div class="dialoginfo">
        <el-form ref="reffromDate" :rules="rules" :inline="true" :model="fromDate" label-width="110px">
          <el-form-item prop="rawId">
            <span slot="label" style="color:#FF0000;">原物料名称</span>
            <el-select v-model="fromDate.rawId" placeholder="请选择">
              <el-option v-for="(item, index) in arr1" :key="index" :label="item.itemName" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="ratio">
            <span slot="label" style="color:#FF0000;">供应比例(%)</span>
            <el-input v-model="fromDate.ratio" type="Number" placeholder="请输入" clearable @input="inputNum(fromDate)"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="resetFromDate">取 消</el-button>
        <el-button class="bottom_all" style="margin-left: 24px;" @click="pagessubmitFromDate()">发起审批</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import mixin from "@/mixins/ICPSmixin.js";
import { queryPage, saveOrUpdate, Download } from "@/api/GHnumber/SPratio.js";
import { getDicType } from "@/api/common.js";
import { getUserAutList } from "@/api/common.js";

export default {
  mixins: [mixin],
  data() {
    return {
      UserInfo: '',
      /*表格增删改查及下载接口*/
      queryPage: queryPage,
      saveOrUpdate: saveOrUpdate,
      DownloadURL: Download,
      /*表格数据*/
      table: [
        { label: "原物料名称", width: "auto", prop: "rawName", status: true },
        { label: "供应比例(%)", width: "auto", prop: "ratio", status: true },
        { label: "操作时间", width: "auto", prop: "updateTime", status: true },
        { label: "操作", width: "120", prop: "", status: true },
      ],
      /*表单页类型，0为无树状，1为有树状，有树状在el-main加上display:flex
       */
      treeData: [],
      activetree: "",
      pagesType: 1,
      /*添加表单数据*/
      rules: {
        rawId: [{ required: true, message: "请选择原物料", trigger: "change" }],
        ratio: [{ required: true, message: "请输入比例", trigger: "blur" }],
      },
      fromDate: {
        stationId: "",
        rawId: "",
        rawName: "",
        ratio: "",
      },
      arr1: [],
      stationName: '',
      oldData: {},
    };
  },
  async created() {
    this.treeData = await getUserAutList()
    this.treeLoading = false;
    this.activetree = this.treeData[0].children[0].value;
    this.stationName = this.treeData[0].children[0].label;

    /*存储拷贝的原初始化fromDate数据*/
    this.tableParam = {
      ...this.tableParam,
      rawId: "",
      stationId: this.activetree,
    };
    await this.gettableData();
    getDicType({ code: "MATname" }).then((res) => {
      this.arr1 = res.data.items;
    });
  },
  watch: {
    activetree: {
      handler() {
        this.tableParam.stationId = this.activetree;
        this.gettableData();
      },
      deep: false,
      immediate: false,
    },
  },
  /*
   *copy(data)深拷贝数据,返回拷贝完的数据
   *gettableData获取列表数据
   *load下载
   *SortNum保持序列号
   * UpdateOrDelete禁用，启用，删除
   * submitFromDate保存更新数据
   */
  methods: {
    inputNum(low) {
      if (Number(low.ratio) > 101) {
        low.ratio = 100
      } else {
        low.ratio = low.ratio.replace(/[^\d]/g, '')
      }

    },
    changeStation(name) {
      this.stationName = name;
    },
    init() {
      /*重置数据*/
      this.tableParam.rawId = "";
      this.gettableData();
    },
    pagessubmitFromDate() {
      let _this = this;
      _this.$refs.reffromDate.validate((valid) => {
        if (valid) {
          _this.fromDate.stationId = _this.activetree;
          _this.fromDate.stationName = _this.stationName;
          _this.fromDate.rawName = _this.$fromatTable(
            _this.arr1,
            _this.fromDate.rawId,
            "id",
            "itemName"
          );
          // _this.submitFromDate();
          this.addDialog = false;
          this.$router.push({ name: 'SiteProportion', query: { ..._this.fromDate, type: 1, oldData: JSON.stringify(this.oldData) } })
        }
      });
    },
    sizeChange(value) {
      this.tableParam.pageSize = value;
      this.gettableData();
    },
  },
};
</script>

<style lang="scss" scoped>
.el-input,
.el-select {
  width: 240px;
}
/*
 <el-link class="table_link" :underline="false">查看配合比</el-link>*/
.table_link {
  margin: 0 10px;
  color: $bg_color;
}

.table_link:hover {
  opacity: 0.8;
}
</style>
